from common.sys.schedule import FilmSchedule
from common.httpd.server import Server
from common.utils.toolkit import Log
from http.server import HTTPServer
import sys,os,getopt,datetime

if __name__ == '__main__':
    mediadir = '/videos'
    rtmpserver = 'stream'
    stream = 'live'
    app = 'movie'
    port = 80
    opts,args = getopt.getopt(sys.argv[1:], '-p:', ['mediadir=', 'rtmp=', 'stream=','app='])
    for optName, optValue in opts:
        if(optName in ['-p']):
            p = int(optValue)
            if(p > 0):
              port = p
        if(optName in ['-meidadir']):
            mediadir = os.path.join(optValue)
        if(optName in ['--rtmp']):
            rtmpserver = optValue
        if(optName in ['--stream']):
            stream = optValue
        if(optName in ['--app']):
            app = optValue
    FilmSchedule().setMediaDir(mediadir)
    FilmSchedule().addNextSchedule()
    Log.i("Welcome, I am listening on %d" % port)
    host = ('0.0.0.0', port)
    server = HTTPServer(host, Server.createHandler(mediadir))
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        server.socket.close
        server.server_close
